﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Game_Engine
{
    public class AnimatedSprite : Sprite
    {
        public float Speed
        {
            get;
            set;
        }

        public int NumFrames
        {
            get;
            set;
        }

        public int CurrentFrame
        {
            get;
            set;
        }

        public int FrameWidth
        {
            get;
            set;
        }

        public float SingleFrameTime
        {
            get;
            set;
        }

        public float TotalTime
        {
            get;
            set;
        }

         public AnimatedSprite(Texture2D texture, int f, float s) : base(texture)
         {
            Speed = s;
            NumFrames = f;
            CurrentFrame = 0;

            FrameWidth = texture.Width / NumFrames;
            SingleFrameTime = NumFrames / Speed;
            TotalTime = 0.0f;
            
            Source = new Rectangle(0, 0, FrameWidth, texture.Height);
            Origin = new Vector2((FrameWidth / 2), Texture.Height / 2);
        }

         public override void Update()
         {
             base.Update();

             TotalTime += Time.ElapsedGameTime;

             if (TotalTime >= SingleFrameTime)
             {
                 CurrentFrame++;
                 CurrentFrame = CurrentFrame % NumFrames;

                 TotalTime -= SingleFrameTime;
             }

             Source = new Rectangle(CurrentFrame*FrameWidth, 0, FrameWidth, Texture.Height);

         }

         public override void Draw(SpriteBatch sB)
         {
             sB.Draw(Texture, Position, Source, Color, Rotation, Origin, Scale, Effects, Depth);
         }
    }
}
